//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "CommodityType.h"
using namespace Cephei::QL::Experimental::Commodities;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (String^ code, String^ name) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCommodityType = NULL;
#endif
        std::string _code = (std::string)ValueHelper::Convert (code);
        std::string _name = (std::string)ValueHelper::Convert (name);
        _ppCommodityType = new boost::shared_ptr<QuantLib::CommodityType> (new QuantLib::CommodityType ( _code,  _name ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phCommodityType = NULL;
#endif
        _ppCommodityType = new boost::shared_ptr<QuantLib::CommodityType> (new QuantLib::CommodityType ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (boost::shared_ptr<QuantLib::CommodityType>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityType = NULL;
#endif
	_ppCommodityType = &childNative;
    
}
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (QuantLib::CommodityType& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phCommodityType = NULL;
#endif
	_ppCommodityType = new boost::shared_ptr<QuantLib::CommodityType> (&childNative);
    
    _CommodityTypeOwner = owner;
    
}

Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (CCommodityType^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityType = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppCommodityType = new boost::shared_ptr<QuantLib::CommodityType> (copy->GetShared());
        
    }
}
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityType = NULL;
#endif
	if (!t->IsSubclassOf(CCommodityType::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (QuantLib::Handle<QuantLib::CommodityType>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommodityType = &childNative;
	_ppCommodityType = &static_cast<boost::shared_ptr<QuantLib::CommodityType>>(childNative.currentLink());
    
    _CommodityTypeOwner = owner;
}
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (QuantLib::Handle<QuantLib::CommodityType> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phCommodityType = &childNative;
	_ppCommodityType = &static_cast<boost::shared_ptr<QuantLib::CommodityType>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Experimental::Commodities::CCommodityType::CCommodityType (QuantLib::CommodityType childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phCommodityType = NULL;
#endif
	_ppCommodityType = new boost::shared_ptr<QuantLib::CommodityType> (new QuantLib::CommodityType (childNative));
    
}
#endif

Cephei::QL::Experimental::Commodities::CCommodityType::~CCommodityType ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppCommodityType != NULL)
    {
	    delete _ppCommodityType;
        _ppCommodityType = NULL;
    }
}
Cephei::QL::Experimental::Commodities::CCommodityType::!CCommodityType ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppCommodityType != NULL)
    {
	    delete _ppCommodityType;
    }
}
QuantLib::CommodityType& Cephei::QL::Experimental::Commodities::CCommodityType::GetReference ()
{
    if (_ppCommodityType == NULL) throw gcnew NativeNullException ();
	return **_ppCommodityType;
}
boost::shared_ptr<QuantLib::CommodityType>& Cephei::QL::Experimental::Commodities::CCommodityType::GetShared ()
{
    if (_ppCommodityType == NULL) throw gcnew NativeNullException ();
	return *_ppCommodityType;
}
QuantLib::CommodityType* Cephei::QL::Experimental::Commodities::CCommodityType::GetPointer ()
{
    if (_ppCommodityType == NULL) throw gcnew NativeNullException ();
	return &**_ppCommodityType;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::CommodityType>& Cephei::QL::Experimental::Commodities::CCommodityType::GetHandle ()
{
	if (_phCommodityType == NULL)
	{
		_phCommodityType = new Handle<QuantLib::CommodityType> (*_ppCommodityType);
	}
	return *_phCommodityType;
}
#endif
bool Cephei::QL::Experimental::Commodities::CCommodityType::HasNative () 
{
	return (_ppCommodityType != NULL);
}

String^ Cephei::QL::Experimental::Commodities::CCommodityType::Code::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppCommodityType)->code ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Experimental::Commodities::CCommodityType::Empty::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppCommodityType)->empty ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
String^ Cephei::QL::Experimental::Commodities::CCommodityType::Name::get ()
{
    try
    {
    	std::string _rv = (std::string)(*_ppCommodityType)->name ( );   
        String^ _nrv = (String^)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Experimental::Commodities::ICommodityType^ Cephei::QL::Experimental::Commodities::CCommodityType_Factory::Create (String^ code, String^ name)
{
    return gcnew CCommodityType ( code,  name);
}
Cephei::QL::Experimental::Commodities::ICommodityType^ Cephei::QL::Experimental::Commodities::CCommodityType_Factory::Create ()
{
    return gcnew CCommodityType ();
}
